home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / c1.zip / ATAN.C < prev    next >
Text File  |  1987-06-18  |  3KB  |  105 lines

  1.  
  2. /***********************************************************
  3.  *               The TULSA IBM C BOARD                     *
  4.  *                   918-664-8737                          *
  5.  *             300/1200 XMODEM, 24 Hours                   *
  6.  **********************************************************/
  7.  
  8.  
  9. #include "libc.h"
  10. #include "math.h"
  11. #include "errno.h"
  12.  
  13. static int nopper() {;}
  14.  
  15. #define PI              3.14159265358979323846
  16. #define PIov2   1.57079632679489661923
  17.  
  18. double atan2(v,u)
  19. double u,v;
  20. {
  21.         double f;
  22.         int (*save)();
  23.         extern int flterr;
  24.         extern int errno;
  25.  
  26.         if (u == 0.0) {
  27.                 if (v == 0.0) {
  28.                         errno = EDOM;
  29.                         return 0.0;
  30.                 }
  31.                 return PIov2;
  32.         }
  33.  
  34.         save = Sysvec[FLT_FAULT];
  35.         Sysvec[FLT_FAULT] = nopper;
  36.         flterr = 0;
  37.         f = v/u;
  38.         Sysvec[FLT_FAULT] = save;
  39.         if (flterr == 2)        /* overflow */
  40.                 f = PIov2;
  41.         else {
  42.                 if (flterr == 1)        /* underflow */
  43.                         f = 0.0;
  44.                 else
  45.                         f = atan(fabs(f));
  46.                 if (u < 0.0)
  47.                         f = PI - f;
  48.         }
  49.         if (v < 0.0)
  50.                 f = -f;
  51.         return f;
  52. }
  53.  
  54. #define P0 -0.13688768894191926929e+2
  55. #define P1 -0.20505855195861651981e+2
  56. #define P2 -0.84946240351320683534e+1
  57. #define P3 -0.83758299368150059274e+0
  58. #define Q0 +0.41066306682575781263e+2
  59. #define Q1 +0.86157349597130242515e+2
  60. #define Q2 +0.59578436142597344465e+2
  61. #define Q3 +0.15024001160028576121e+2
  62.  
  63. #define P(g) (((P3*g P2)*g P1)*g P0)
  64. #define Q(g) ((((g Q3)*g Q2)*g Q1)*g Q0)
  65.  
  66. double atan(x)
  67. double x;
  68. {
  69.         double f, r, g;
  70.         int n;
  71.         static double Avals[4] = {
  72.                 0.0,
  73.                 0.52359877559829887308,
  74.                 1.57079632679489661923,
  75.                 1.04719755119659774615
  76.         };
  77.  
  78.         n = 0;
  79.         f = fabs(x);
  80.         if (f > 1.0) {
  81.                 f = 1.0/f;
  82.                 n = 2;
  83.         }
  84.         if (f > 0.26794919243112270647) {
  85.                 f = (((0.73205080756887729353*f - 0.5) - 0.5) + f) /
  86.                                 (1.73205080756887729353 + f);
  87.                 ++n;
  88.         }
  89.         if (fabs(f) < 2.3e-10)
  90.                 r = f;
  91.         else {
  92.                 g = f*f;
  93.                 r = f + f *
  94.                         ((P(g)*g)
  95.                         /Q(g));
  96.         }
  97.         if (n > 1)
  98.                 r = -r;
  99.         r += Avals[n];
  100.         if (x < 0.0)
  101.                 r = -r;
  102.         return r;
  103. }
  104.  
  105.